package com.nbm.http;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class Httpclient
{
        /**
         * @param password
         * @param name
         * @return message
         * @throws IOException
         * @throws ClientProtocolException
         * 
         */
        public static String HttpLogin(String name, String password)
                        throws ClientProtocolException, IOException
        {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpget = new HttpPost(
                                "http://60.27.214.29:8080/j_signon_check.jsp?loginName=" + name
                                                + "&password=" + password);
                HttpResponse response = httpclient.execute(httpget);
                Header[] hs = response.getAllHeaders();
                HttpEntity entity = response.getEntity();
                if (entity != null)
                {
                        InputStream instream = entity.getContent();
                        byte[] tmp = new byte[2048];
                        while ((instream.read(tmp)) != -1)
                        {
                                System.out.println(new String(tmp, "UTF-8"));
                        }
                }
                for (Header h : hs)
                {
                        System.out.println(h.toString());
                }

                String s = new String();
                HttpPost httpget2 = new HttpPost(
                                "http://60.27.214.29:8080/private/message/MessageClient!getMessages");
                HttpResponse response2 = httpclient.execute(httpget2);
                Header[] hs2 = response2.getAllHeaders();
                for (Header h : hs2)
                {
                        System.out.println(h.toString());
                }
                HttpEntity entity2 = response2.getEntity();
                if (entity2 != null)
                {
                        InputStream instream = entity2.getContent();
                        byte[] tmp = new byte[30720];
                        while ((instream.read(tmp)) != -1)
                        {
                                String s1 = new String(tmp, "UTF-8");
                                s = s + s1;
                        }
                }
                return s;
                // httpclient会自动提交cookie！！
        }

        public static void main(String[] args) throws ClientProtocolException, IOException
        {
                HttpLogin("zongjingli", "zongjingli");
                // HttpLogin("yuangong1", "yuangong1");
                // System.out.println("end");
        }
}
